package com.newhero.product.module.sys.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.newhero.product.common.controller.BaseController;
import com.newhero.product.common.mybatis.mapper.EntityWrapper;
import com.newhero.product.common.util.StringUtils;
import com.newhero.product.common.vo.Json;
import com.newhero.product.common.mybatis.plugins.Page;
import com.newhero.product.module.sys.model.Dict;
import com.newhero.product.module.sys.service.IDictService;
import com.newhero.product.util.ExportUtil;

 /**
 * @ClassName: SysDictController
 * @Description: 访问控制类
 * @author Caolj
 * @date 2018-03-29
 */
@Controller
@RequestMapping("/admin")
public class DictController extends BaseController {
    
    @Autowired 
    private IDictService sysDictServiceImpl;
    																	
    /**
     * 列表页面
     * @return
     */    
	@RequestMapping(value = "/dictList", method ={RequestMethod.GET,RequestMethod.POST })
	public String dictList(
			@RequestParam(value = "page", defaultValue = "1", required = true) int page,// 页号
			@RequestParam(value = "rows", defaultValue = "100", required = true) int rows,// 每页显示的记录条数
			@RequestParam(value = "orderField", defaultValue = "DICTORDER", required = true) String orderField,
			@RequestParam(value = "isAsc", defaultValue = "1", required = true) int isAsc,//1-代表true正序;0-代表false倒序
			Dict sysDict,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		Page<Dict> p = new Page<Dict>();
		p.setCurrent(page);
		p.setSize(rows);
		p.setOrderByField(orderField);
		if(isAsc==0){
			p.setAsc(false);
		}else{
			p.setAsc(true);
		}
		
		EntityWrapper<Dict> entityWrapper = new EntityWrapper<Dict>();
	
		p = sysDictServiceImpl.selectPage(p, entityWrapper);
		model.addAttribute("data", p);
		model.addAttribute("orderField", orderField);
		model.addAttribute("isAsc", isAsc);
		
		return "/sys/dict/dictList";
	}
	
    /**
     * 表单页面
     * @return
     */
	@RequestMapping(value = "/sysDictForm", method = RequestMethod.GET)
	public String sysDictForm(String dictId, HttpServletRequest request) {
		if (dictId != null && !"".equals(dictId)) {
			request.setAttribute("dictId", dictId);
			request.setAttribute("formData",sysDictServiceImpl.selectById(dictId));
		} else {
			request.setAttribute("dictId", "");
			request.setAttribute("formData", new HashMap<String, Object>());
		}
		return "/sys/sysDict/sysDictForm";
	}
    
    /**
     * 表单保存
     * @return
     */
	@RequestMapping(value = "/sysDictSave", method = RequestMethod.POST)
	public String sysDictSave(
			String dictId, Dict sysDict, 
			HttpServletRequest request,HttpServletResponse response, ModelMap model) {
		try {
			if (!StringUtils.isBlank(dictId)) {
				model.addAttribute("flag", "数据修改成功！");
				sysDictServiceImpl.updateById(sysDict);
			} else {
				model.addAttribute("flag", "数据添加成功！");
				sysDictServiceImpl.insert(sysDict);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		return "redirect:sysDictList";
	}

    /**
     * 删除
     * @param ids
     * @return
     */
	@RequestMapping(value = "/sysDictDelete", method = { RequestMethod.POST, RequestMethod.GET })
	public String sysDictDelete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String[] idArr = ids.split(",");
		sysDictServiceImpl.deleteBatchIds(Arrays.asList(idArr));
		model.addAttribute("flag", "数据删除成功！");
		return "redirect:sysDictList";
	}     
    
    
    @ResponseBody
	@RequestMapping(value = "/sysDictAjax", method = RequestMethod.POST)
	public Json sysDictAjax(String dictId) {
		Dict sysDict = sysDictServiceImpl.selectById(dictId);
		Json json = new Json();
		json.setSuccess(true);
		json.setObj(sysDict);
		return json;
	}
	
	@RequestMapping(value = "/sysDictExportExcel", method = { RequestMethod.GET, RequestMethod.POST })
	public void sysDictExportExcel(
			Dict sysDict,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		
	}
}
